package com.learn.profile;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Arrays;
import java.util.List;

/**
 * Maven 之 profile 与 Spring boot 的 profile
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2024/5/19 12:48
 * @updateUser: MI
 * @updateTime: 2024/5/19 12:48
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Slf4j
@SpringBootApplication
public class MavenProfileApplication {


    private static final List<String> envList = Arrays.asList("local", "dev", "test", "pre", "prod");

    /**
     * 配置文件的前缀
     */
    private static final String configPrefix = "/application-";

    /**
     * 配置文件的后缀
     */
    private static final String configSuffix = ".yaml";


    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(MavenProfileApplication.class, args);
        Environment env = context.getEnvironment();
        String configFileName = configFileName(env);
        log.info("[获取相应的配置文件名:]########################configFileName:{}", configFileName);
    }

    /**
     * 根据当前环境 获取相应的配置文件名
     *
     * @return
     */
    private static String configFileName(Environment env) {
        // 默认local环境
        String profiles = "local";
        String[] activeProfiles = env.getActiveProfiles();
        for (int i = 0; i < activeProfiles.length; i++) {
            if (envList.contains(activeProfiles[i])) {
                profiles = activeProfiles[i];
            }
        }
        return configPrefix + profiles + configSuffix;
    }

}
